package com.dy.nygl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dy.nygl.pojo.EquipMonit;
import com.dy.nygl.service.EquipMonitService;

@Controller
@RequestMapping("/equip")
public class EquipMonitController {

	private Integer line = 10;
	private String select1 = "";
	private String search = "";
	private String year = "";
	private String month = "";
	private String day = "";
	private int startLine = 0;
	private int endLine;
	private List<EquipMonit> equipList;

	@Resource(name = "equipMonitService")
	private EquipMonitService equipMonitService;

	@RequestMapping("/setDateList.action")
	public void setDateList(HttpServletResponse response, HttpSession session) throws IOException {
		String year = getYear();
		String month = getMonth();
		String day = getDay();
		equipList = equipMonitService.selectByDateLike(year, month, day);
		setEquipList(equipList);
		cleanLine();
		clean();
		PrintWriter out = response.getWriter();
		out.print(3);
	}

	@RequestMapping("/getList.action")
	public ModelAndView getList(HttpSession session) throws IOException {
		ModelAndView model = new ModelAndView();

		startLine = getStartLine();
		model.addObject("indexOf", startLine);
		line = getline();
		endLine = startLine + line;
		model.addObject("line", endLine);

		equipList = getEquipList();

		model.addObject("equipList", equipList);
		model.setViewName("equip_monitor");

		return model;

	}

	@RequestMapping("/nextPage.action")
	public void nextPage(@RequestParam("end") Integer end, HttpServletResponse response, HttpSession session)
			throws IOException {
		
		setStartLine(end + 1);
		PrintWriter out = response.getWriter();
		out.print(1);
	}

	@RequestMapping("/lastPage.action")
	public void lastPage(@RequestParam("begin") Integer begin, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {

		

		line = getline();
		if (begin - line < 0) {
			startLine = begin;
			setStartLine(startLine);
			endLine = startLine + line;
			setEndLine(endLine);

		} else {
			startLine = begin - line;
			setStartLine(startLine);
			endLine = startLine + line;
			setEndLine(endLine);
		}

		PrintWriter out = response.getWriter();
		out.print(8);

	}

	@RequestMapping("/setList.action")
	public void setAllList(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		equipList = equipMonitService.selectAll();
		setEquipList(equipList);
		PrintWriter out = response.getWriter();
		out.print(6);
	}

	@RequestMapping("/setIndex.action")
	public void setIndex(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		setStartLine(0);
		PrintWriter out = response.getWriter();
		out.print(9);

	}

	@RequestMapping(value = "/changeline.action", method = { RequestMethod.POST })
	public void checkLine(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String line = request.getParameter("line");
		setline(line);
		out.print(1);
	}

	public void cleanLine() {
		this.startLine = 0;
		this.endLine = 0;
	}

	@RequestMapping("/searchEquip.action")
	public void getList1(HttpServletResponse response, HttpSession session) throws ServletException, IOException {
		cleanList();
		cleanLine();
		String sect = getSect1();
		String search = getSearch();
		equipList = equipMonitService.selectEquip(sect, search);
		setEquipList(equipList);

		PrintWriter out = response.getWriter();
		out.print(5);

	}

	public void cleanList() {
		equipList = null;
	}

	@RequestMapping("/getDateLike.action")
	public ModelAndView getDate(HttpServletResponse response, HttpSession session) throws IOException {

		ModelAndView model = new ModelAndView();
		clean();
		cleanLine();
		startLine = getStartLine();
		model.addObject("indexOf", startLine);
		line = getline();
		endLine = startLine + line;
		model.addObject("line", endLine);
		equipList = getEquipList();
		model.addObject("equipList", equipList);
		model.setViewName("equip_monitor");

		return model;

	}

	public void returnPrint(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		out.print(2);
	}

	public void clean() {
		this.year = "";
		this.month = "";
		this.day = "";
	}

	@RequestMapping("/updateThis.action")
	public ModelAndView updateThis(@RequestParam("id") Integer id, HttpSession session) throws IOException {

		ModelAndView model = new ModelAndView();
		model.addObject("id", id);
		model.setViewName("equipment");
		return model;
	}

	@RequestMapping(value = "/insertSelective.action", method = { RequestMethod.POST })
	public ModelAndView insertSelective(HttpServletRequest request, HttpSession session)
			throws ServletException, IOException {

		String equipId = request.getParameter("equipId");
		String equipName = request.getParameter("equipName");
		String totalConsum = request.getParameter("totalConsum");
		String referValue = request.getParameter("referValue");
		String currentConsum = request.getParameter("currentConsum");

		EquipMonit equipMonit = new EquipMonit();
		equipMonit.setEquipName(equipName);
		equipMonit.setEquipId(equipId);
		equipMonit.setTotalConsum(totalConsum);
		equipMonit.setReferValue(referValue);
		equipMonit.setCurrentConsum(currentConsum);

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// �������ڸ�ʽ
		String date = df.format(new Date());
		equipMonit.setDate(date);

		equipMonitService.insert(equipMonit);

		ModelAndView model = new ModelAndView();

		startLine = getStartLine();
		model.addObject("indexOf", startLine);
		line = getline();
		endLine = startLine + line;
		model.addObject("line", endLine);

		equipList = getEquipList();

		model.addObject("equipList", equipList);
		model.setViewName("equip_monitor");

		return model;

	}

	@RequestMapping(value = "/updateSelective.action", method = { RequestMethod.POST })
	public ModelAndView updateSelective(HttpServletRequest request, HttpSession session) throws IOException {

		String id1 = request.getParameter("id");
		int id = Integer.parseInt(id1);
		String equipId = request.getParameter("equipId");
		String equipName = request.getParameter("equipName");
		String totalConsum = request.getParameter("totalConsum");
		String referValue = request.getParameter("referValue");
		String currentConsum = request.getParameter("currentConsum");

		EquipMonit equipMonit = new EquipMonit();
		equipMonit.setId(id);
		equipMonit.setEquipName(equipName);
		equipMonit.setEquipId(equipId);
		equipMonit.setTotalConsum(totalConsum);
		equipMonit.setReferValue(referValue);
		equipMonit.setCurrentConsum(currentConsum);
		equipMonitService.update(equipMonit);

		List<EquipMonit> equipList = new ArrayList<>();
		ModelAndView model = new ModelAndView();
		equipList = equipMonitService.selectAll();
		model.addObject("indexOf", 0);
		line = getline();
		model.addObject("line", line);
		model.addObject("equipList", equipList);
		model.setViewName("equip_monitor");

		return model;
	}

	@RequestMapping(value = "/getSearch.action", method = { RequestMethod.POST })
	public void getSearch(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String search = request.getParameter("search");
		setSearch(search);
		out.print(2);
	}

	@RequestMapping(value = "/getSect1.action", method = { RequestMethod.POST })
	public void getSect1(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String sect1 = request.getParameter("sect1");
		setSect1(sect1);
		out.print(3);
	}

	@RequestMapping(value = "/getYear.action", method = { RequestMethod.POST })
	public void getYear(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String year = request.getParameter("year");
		setYear(year);
		out.print(5);
	}

	@RequestMapping(value = "/getMonth.action", method = { RequestMethod.POST })
	public void getMonth(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String month = request.getParameter("month");
		setMonth(month);
		out.print(6);
	}

	@RequestMapping(value = "/getDay.action", method = { RequestMethod.POST })
	public void getDay(HttpServletRequest request, HttpServletResponse response, HttpSession session)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String day = request.getParameter("day");
		setDay(day);
		out.print(7);
	}

	public void setSect1(String sect1) {
		this.select1 = sect1;
	}

	public String getSect1() {
		return select1;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getSearch() {
		return search;
	}

	public void setline(String line) {
		int line1 = Integer.parseInt(line);
		this.line = line1;
	}

	public int getline() {
		return line;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getMonth() {
		return month;
	}

	public void setMonth(String month) {
		this.month = month;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	public int getStartLine() {
		return startLine;
	}

	public void setStartLine(int startLine) {
		this.startLine = startLine;
	}

	public int getEndLine() {
		return endLine;
	}

	public void setEndLine(int endLine) {
		this.endLine = endLine;
	}

	public List<EquipMonit> getEquipList() {
		return equipList;
	}

	public void setEquipList(List<EquipMonit> equipList) {
		this.equipList = equipList;
	}

	@RequestMapping("/update.action")
	public void update() {
		EquipMonit equipMonit = new EquipMonit();
		for (int i = 363; i < 393; i++) {
			equipMonit.setId(i);
			equipMonit.setName("name" + i);
			equipMonit.setEquipName("Equip-Name-" + i);
			equipMonit.setEquipId("Equip-No." + i);
			equipMonit.setTotalConsum("666" + i);
			equipMonit.setReferValue("28" + i);
			equipMonit.setCurrentConsum("235." + i);
			int j = i - 362;
			equipMonit.setDate("2016-12-" + j);
			equipMonitService.update(equipMonit);

		}

	}

	@RequestMapping("/insert.action")
	public void insert() {
		EquipMonit equipMonit = new EquipMonit();
		for (int j = 1; j < 32; j++) {
			equipMonit.setId(j);
			equipMonit.setName("Name-" + j);
			equipMonit.setEquipName("EquipName-" + j);
			equipMonit.setEquipId("Equip-No." + j);
			equipMonit.setReferValue("28" + j);
			equipMonit.setTotalConsum("666" + j);
			equipMonit.setCurrentConsum("235." + j);
			equipMonit.setDate("2016-1-" + j);
			equipMonitService.insert(equipMonit);

		}

	}

	@RequestMapping("/deleteThis.action")
	public void deleteThis(@RequestParam("id") Integer id,HttpServletResponse response,HttpSession session) throws ServletException,IOException {

		ModelAndView model = new ModelAndView();
		equipMonitService.deleteThis(id);
		equipList = equipMonitService.selectAll();
		setEquipList(equipList);
		PrintWriter out=response.getWriter();
		out.print(1);
	}

}
